import { Injectable } from "@nestjs/common";
import { DBReaderRepository } from "../repository/db-reader.repository";
/* 
服务层负责调用数据库查询语句获取数据并对数据进行处理
所有的数据库查询语句应放在循环体外面，先将要处理的数据查询出来，然后再进行计算，这会大大提高查询的效率。
所有的数据库查询操作在repository文件夹下的db-reader.repository.ts文件中
*/
@Injectable()
export class VisualService {
    constructor(
        private readonly dbReaderRepository: DBReaderRepository,
    ) { }

    async test() {
        return await this.dbReaderRepository.test();
    }

    async insert_card_for_power(leftTime, rightTime) {
        const listData = await this.dbReaderRepository.insert_card_for_power_data(leftTime, rightTime);
        console.log(listData);
        const hotelRoomData = await this.getHotelRoomData(null);

        var arrs = [];
        for (var i = 0; i < listData.length; i++) {
            for (var key in hotelRoomData) {//hotelRoomData.length，只遍历1，2，3，6，7，8
                if (listData[i].registerId in hotelRoomData[key]) {
                    arrs.push({
                        'registerId': listData[i].registerId,
                        'hotelname': hotelRoomData[key].hotelname,
                        'roomname': hotelRoomData[key][listData[i].registerId],
                        'invalid': listData[i].invalid || 0,
                        'sumcount': listData[i].sumcount
                    });
                    break;
                }
            }
        }
        // console.log(arrs) 
        return arrs;//使数据按某种规范格式输出

    }


    async getHotelRoomData(hotelId: number) {
        const data1 = await this.dbReaderRepository.getHotelRoomDataOne();
        const data2 = await this.dbReaderRepository.getHotelRoomDataTwo(hotelId);

        let arrs = {}, index1 = 0, index2 = 0;
        // console.log("1",sql2)
        for (let i = 0; i < data1.length; i++) {//data2中44号存在数据，45号不存在
            while (data1[i].customerId > data2[index1].customerId) {
                if (++index1 >= data2.length) return arrs;
            }
            if (data1[i].customerId < data2[index1].customerId) continue;
            else {
                index2 = index1;
                while (true) {
                    if (data1[i].roomname != data2[index2].roomname) {
                        if (++index2 >= data2.length || data1[i].customerId != data2[index2].customerId) break;
                    } else if (data1[i].roomname == data2[index2].roomname) {
                        if (arrs[data2[index2].id]) {
                            arrs[data2[index2].id][data1[i].registerId] = {
                                'roomname': data1[i].roomname,
                                'rest_netId': data1[i].rest_netId
                            }
                            break;
                        } else {
                            arrs[data2[index2].id] = {
                                'hotelname': data2[index2].hotelname
                            }
                        }
                    }
                }
            }
        }
        return arrs;//{hotelId:{'hotelname':,'reId':{roomname,rest_netId}...}

    }
    // insert_card_for_power('2022-07-04 21:02','2022-07-05 21:04');
    async get_hotel_daily_settel(leftTime, rightTime) {//根据时间段查询酒店日租信息
        const data = await this.dbReaderRepository.get_hotel_daily_settel(leftTime, rightTime);
        // console.log(data);
        let arrs = [], arrs2 = [];
        let index = 0;
        for (let i = 0; i < data.length; i++) {
            var lived = parseInt(data[i].desc.substring(data[i].desc.indexOf("（共：") + 3, data[i].desc.indexOf("间）")));
            arrs.push({
                amount: data[i].amount,
                from: data[i].from.getFullYear() + "-" + (data[i].from.getMonth() + 1) + "-" + data[i].from.getDate(),
                isSettled: data[i].isSettled,
                name: data[i].name,
                number: lived
            });
            if (data[i].hotelId != index) {
                index = data[i].hotelId;
                arrs2.push({//一条记录时情况
                    'key': data[i].hotelId,
                    'name': data[i].name,
                    'hotelRoomNumber': data[i].roomnum,
                    'livedNumberInTime': lived,
                    'hotelTotalRoomNumberInTime': data[i].roomnum,
                    'livedRateInTime': lived / data[i].roomnum,
                    'costInTime': data[i].amount,
                    'lastIncomeInTime': data[i].amount,
                    'getIncomeInTime': (data[i].isSettled === 1) ? data[i].amount : 0,
                    'notGetIncomeInTime': (data[i].isSettled === 0) ? data[i].amount : 0
                })
            } else {//多天情况
                arrs2[arrs2.length - 1].livedNumberInTime += lived;
                arrs2[arrs2.length - 1].hotelTotalRoomNumberInTime += data[i].roomnum;
                arrs2[arrs2.length - 1].livedRateInTime = arrs2[arrs2.length - 1].livedNumberInTime / arrs2[arrs2.length - 1].hotelTotalRoomNumberInTime;
                arrs2[arrs2.length - 1].costInTime += data[i].amount;
                arrs2[arrs2.length - 1].lastIncomeInTime += data[i].amount;
                if (data[i].isSettled === 1) arrs2[arrs2.length - 1].getIncomeInTime += data[i].amount;
                else arrs2[arrs2.length - 1].notGetIncomeInTime += data[i].amount;
            }
        }
        return [arrs, arrs2];
    }

    async rent_hotel_data() {
        let date = new Date();
        const data = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        const renthotelroomnum = await this.dbReaderRepository.getRenthotelnum(data);
        const renthotelnum = await this.dbReaderRepository.getRenthotelnum(data);
        const hotelnum = await this.dbReaderRepository.getHotelnum();
        return true;

    }
    async occupancy_rate() {
        return true;
    }


    async install_room(leftTime, rightTime) {
        const data = await this.dbReaderRepository.install_room(leftTime, rightTime);
        let arrs = {}, index = 0;
        var left = new Date(leftTime);
        var right = new Date(rightTime);
        while (left < right) {
            left = new Date(left);
            left.setDate(left.getDate() + 1);
            if (index == data.length || data[index].from > left) {
                arrs[left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate()] = 0;
            } else {
                arrs[left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate()] = data[index++].installnum;
            }
        }
        let arrs1 = {};
        // var timePeriod = timesPeriod(leftTime, rightTime);//判断时间单位shi月、周、日
        // for (var key in data) {
        //     if (timePeriod == '月') var fromtime = key.substring(0, key.lastIndexOf('-'));
        //     else if (timePeriod == '周') var fromtime = sundayFromdate(key);//将日期转换成本周周日或一
        //     else var fromtime = key;
        //     if (arrs[fromtime]) arrs[fromtime].installnum += data[key];
        //     else arrs[fromtime] = { 'from': fromtime, 'installnum': data[key] || 0 }
        // }


    }

    weeks7(right) {//获取本周周日日期或上周周日日期
        if (right.indexOf('23:59:59') < 0) {
            var date = new Date();
            switch (date.getDay()) {
                case 1: date = new Date(date.setDate(date.getDate() + 6));
                    break;
                case 2: date = new Date(date.setDate(date.getDate() + 5));
                    break;
                case 3: date = new Date(date.setDate(date.getDate() + 4));
                    break;
                case 4: date = new Date(date.setDate(date.getDate() + 3));
                    break;
                case 5: date = new Date(date.setDate(date.getDate() + 2));
                    break;
                case 6: date = new Date(date.setDate(date.getDate() + 1));
                    break;
            }
            right = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' 23:59:59';
        } else right = right.substring(1, right.length - 1);
        let left = new Date(right);
        left = new Date(left.setDate(left.getDate() - 7));
        const leftTime = '\'' + left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59' + '\'';
        right = '\'' + right + '\'';
        return [leftTime, right];
    }

    time_transition(time) {//将类如今天转换从具体时间段
        let right = new Date();
        let left = new Date();

        let leftTime;
        let rightTime;
        if (time === '今年') {
            left.setMonth(0); left.setDate(0);//去年最后一天
            rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
            leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';


        } else if (time === '去年') {//去年，前年，没有数据
            right.setMonth(0); right.setDate(0);//去年最后一天
            left = new Date(right); left.setMonth(0); left.setDate(0);//前年最后一天
            rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
            leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';
        } else if (time.length > 4) {
            var date = time.split(',');
            left = date[0]; right = date[1];

        } else {
            if (time === '今天' || time === '今日') {
                rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
                left.setDate(left.getDate() - 1);
                leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';
            } else if (time === '昨日' || time === '昨天') {//今天向前推7天,变成推前8天
                right.setDate(right.getDate() - 1);
                rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
                left = new Date(left.setDate(left.getDate() - 2));
                leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';
                // console.log("昨天",left,right);

            } else if (time === '本周') {
                let date = this.weeks7("");//本周时间
                left = date[0].substring(1, date[0].length - 1); right = date[1].substring(1, date[1].length - 1);
            } else if (time === '上周') {
                let date = this.weeks7(this.weeks7("")[0]);//上周时间
                left = date[0].substring(1, date[0].length - 1); right = date[1].substring(1, date[1].length - 1);

            } else if (time === '本月') {
                left.setDate(0);//上月最后一天
                rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
                leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';

            } else {//上月
                right = new Date(right.setDate(0));//上个月最后一天
                left = new Date(right);
                left.setDate(0);//上上个月最后一天
                rightTime = right.getFullYear() + '-' + (right.getMonth() + 1) + '-' + right.getDate() + ' 23:59:59';
                leftTime = left.getFullYear() + '-' + (left.getMonth() + 1) + '-' + left.getDate() + ' 23:59:59';

            }
        }
        return [left, right];
    }



}